package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.dto.OrgDTO;
import com.sihan.framework.etm.entity.Org;
import com.sihan.framework.etm.service.OrgService;
import com.sihan.framework.etm.vo.OrgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import javax.validation.Valid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "单位管理")
@RestController
@RequestMapping("/system/v0/org")
public class AdminOrgController {


  @Autowired
  private OrgService orgService;

  @ApiOperation(value = "获得所有部门")
  @GetMapping("/list")
  @BusinessLog(value = "获得所有部门(数组形式)")
  public ApiResult<?> orgList() {
    List<OrgVO> result = orgService.orgList();
    return ApiResult.success(result);
  }

  @ApiOperation(value = "获得所有部门(树状形式)")
  @GetMapping("/tree")
  @BusinessLog(value = "获得所有部门(树状形式)")
  public ApiResult<?> orgTree() {
    List<OrgVO> maps= orgService.orgTree();
    return ApiResult.success(maps);
  }
  
  @ApiOperation(value = "根据公司获得所有部门(树状形式)")
  @GetMapping("/bycompany/tree")
  @BusinessLog(value = "根据公司获得所有部门(树状形式)")
  public ApiResult<?> getDepByCompany(@ApiParam(name = "companyId", value = "公司Id", required = true) @RequestParam String companyId) {
	  List<OrgVO> result = orgService.getDepByCompany(companyId);
    return ApiResult.success(result);
  }

  @ApiOperation(value = "保存单位")
  @PostMapping("/save")
  @BusinessLog(value = "保存单位信息")
  public ApiResult<?> saveOrg(@Valid @ApiParam(name = "orgDto", value = "单位数据", required = true) 
  	@RequestBody OrgDTO orgDto) {
    Org org = new Org();
    BeanUtils.copyProperties(orgDto, org);
    orgService.saveOrg(org);
    return ApiResult.success();
  }

  @ApiOperation("删除单位")
  @PostMapping("/delete")
  @BusinessLog(value = "删除单位")
  public ApiResult<?> deleteOrg(@ApiParam(name = "orgIds", value = "单位Ids", required = true) @RequestBody List<String> orgIds) {

      orgService.deleteOrg(orgIds);
      return ApiResult.success();
  }
  
  @ApiOperation(value = "下拉组织机构")
  @GetMapping("/queryByIdOrPId")
  @BusinessLog(value = "根据id或者父id查询")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "id", value = "组织结构id", dataType = "int", paramType = "query", required = false),
          @ApiImplicitParam(name = "parentId", value = "组织机构父id", dataType = "int", paramType = "query", required = false)
  })
  public ApiResult<?> listOrgsByIdOrPId(OrgDTO dto) {
    List<OrgVO> result = orgService.listOrgByIdOrPId(dto);
    return ApiResult.success(result);
  }


  @ApiOperation(value = "根据当前用户获取下拉组织机构")
  @GetMapping("/queryByUser")
  @BusinessLog(value = "根据当前用户获取下拉组织机构")
  @RequiresPermissions("sys:manager:role:menu")
  public ApiResult<?> queryByUser() {
    return ApiResult.success(orgService.queryByUser());
  }


}
